package com.katze.ssh.sftp;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class SftpDownloadTask implements AutoCloseable{

    private final String id;
    private int total = 0;
    private int count = 0;
    private ZipEntity current;
    private boolean doing = true;
    private boolean loading = true;
    private final BlockingQueue<ZipEntity> queue;

    public SftpDownloadTask(String id) {
        this.id = id;
        this.queue = new LinkedBlockingQueue<>();
    }

    public String getId() {
        return id;
    }

    public boolean isLoading() {
        return loading;
    }

    public boolean isDoing() {
        return doing;
    }

    public void add(ZipEntity item) {
        total ++;
        queue.add(item);
    }

    public ZipEntity poll() {
        if (loading) {
            this.loading = false;
        }
        return this.current = queue.poll();
    }

    public void done() {
        count ++;
    }

    @Override
    public String toString() {
        return "{\"filename\":\"" + current.getName() + "\",\"progress\":" + current.getProgress() + ",\"total\":" + total +",\"count\":" + count + "}";
    }

    @Override
    public void close() throws Exception {
        this.doing = false;
    }
}
